<script setup>
import useWebsiteList from "@/hooks/list/useWebsiteList";
import useIndex from "@/hooks/list/useIndex";
import { computed, inject } from "vue";
const { useWebsite } = useWebsiteList();
const { currentIndex, handleIndex } = useIndex();
const { keywordRef } = inject("search-keyword");
const handleDeleteWebsite = (ws) => {
  useWebsite.deleteWebsite(ws.url);
  currentIndex.value = 0;
}
</script>

<template>
  <div class="app-container">
    <p id="no-item" v-if="useWebsite.websites(keywordRef).length === 0">暂无数据...</p>
    <div class="items" v-else>
      <div :class="['read-item', currentIndex === index ? 'selected':null]" v-for="(ws,index) in useWebsite.websites(keywordRef)" :key="ws.url" @click="handleIndex(index,ws.url)">
        <img :src="ws.screenshot" :alt="ws.title" />
        <h3>{{ws.title}}</h3>
        <button @click.stop="handleDeleteWebsite(ws)"> X </button>
      </div>
    </div>
  </div>
</template>
<style lang="scss" scoped>
.app-container {
  #no-item {
    position: absolute;
    top: 100px;
    width: 100%;
    font-weight: bold;
    color: silver;
    text-align: center;
    z-index: -1;
  }
  .items {
    .read-item {
      display: flex;
      align-items: center;
      align-content: center;
      border-bottom: lightgray 2px solid;
      background: #fafafa;
      border-left: 10px solid lightgray;
      -webkit-user-select: none;
      position: relative;
      padding: 10px;
      img {
        width: 20%;
        margin-right: 25px;
        border: solid 1px #ccc;
      }

      &:hover {
        background: #eee;
        button {
          display: inline-block;
        }
      }
      &.selected {
        border-left-color: dodgerblue;
      }
      button {
        position: absolute;
        display: none;
        right: 10px;
        top: 10px;
        width: 30px;
        height: 30px;
        background: #f44336;
        border: none;
        border-radius: 50%;
        color: white;
        text-align: center;
        font-size: 16px;
        cursor: pointer;
      }
    }
  }
}
</style>